
local assert     = assert
local require    = require
local bit        = bit
local coroutine  = coroutine
local debug      = debug
local io         = io
local pairs      = pairs
local ipairs     = ipairs
local math       = math
local os         = os
local print      = print
local pcall      = pcall
local xpcall     = xpcall
local rawget     = rawget
local rawset     = rawset
local select     = select
local string     = string
local table      = table
local tonumber   = tonumber
local tostring	 = tostring
local error      = error
local type       = type
local unpack     = unpack
local setmetatable = setmetatable
local getmetatable = getmetatable
local ngx = ngx

local string_char  = string.char
local string_sub   = string.sub
local string_byte  = string.byte
local string_gsub  = string.gsub
local string_find  = string.find
local string_upper = string.upper

local math_fmod   = math.fmod
local math_floor  = math.floor
local math_pow    = math.pow
local math_abs    = math.abs

local table_insert = table.insert

local os_date = os.date;
local table_concat = table.concat;


local uu         = require("modules.commhm.utils");
local log        = require("modules.commhm.log");
local ns_time    = require("modules.commhm.time");
local ns_network = require("modules.commhm.network");
local ns_utf8    = require("modules.commhm.utf8");


local CHtmlTable = require( "modules.html_page.CHtmlTable" )


local table_tostring = table.tostring
local table_loadstring = table.loadstring



local FOO = {
	_VERSION = '0.12.1',

	show_txt = {},
};


FOO.header = [[<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>sens_checker</title>
<script src="https://indevelop.mini1.cn:8088/oa/js_lib/angular.min.js"></script>
<script src="https://indevelop.mini1.cn:8088/oa/js_lib/cookie.min.js"></script>
<script src="https://indevelop.mini1.cn:8088/oa/js_lib/md5.js"></script>
<script src="https://indevelop.mini1.cn:8088/oa/js_lib/jquery-3.1.1.min.js"></script>
<script src="https://indevelop.mini1.cn:8088/oa/js_lib/miniajax.js"></script>
</head>]];


FOO.header_https = [[<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>sens_checker</title>
<script src="https://indevelop.mini1.cn:8088/oa/js_lib/angular.min.js"></script>
<script src="https://indevelop.mini1.cn:8088/oa/js_lib/cookie.min.js"></script>
<script src="https://indevelop.mini1.cn:8088/oa/js_lib/md5.js"></script>
<script src="https://indevelop.mini1.cn:8088/oa/js_lib/jquery-3.1.1.min.js"></script>
<script src="https://indevelop.mini1.cn:8088/oa/js_lib/miniajax.js"></script>
</head>]];



FOO.goto_https = [[
<body>

<br>
<br>
<br>

按照公司安全规定，您需要进行权限验证（点击下面链接进行权限验证）。

<br>
<br>
<a href="https://120.24.64.132:8002/oa/CheckSensWord.php" title="goto"> https://120.24.64.132:8002/oa/CheckSensWord.php </a>
<br>
</body></html>
]]




--显示页面
FOO.show = function ( env_, is_https_ )
	FOO.clean_txt()

	if  is_https_ then
		FOO.add_txt( FOO.header_https )
	else
		FOO.add_txt( FOO.header )

		FOO.add_txt( FOO.goto_https )

		ngx.say( FOO.get_txt() )

		return
	end



	FOO.add_txt( '<body>' )

	FOO.add_txt( '敏感词查询页, 当前环境：' .. env_ .. ' <br> <hr>' )

	FOO.add_txt( '<table border=1 cellpadding=5>' )
	FOO.add_txt( '<tr>' )

	local active_style_ = 'style="background-color:#00ffff"'

	local txt_ = [[
<td><button @@1@@ onclick="change_env(1)" >开发环境1</button>&nbsp;</td>
<td><button @@2@@ onclick="change_env(2)" >先遣环境2</button>&nbsp;</td>
<td><button @@0@@ onclick="change_env(0)" ><font color="#FF0000"><b>正式环境0</b></font></button>&nbsp;
&nbsp;&nbsp;</td>
<td><button @@12@@ onclick="change_env(12)" >海外先遣环境12</button>&nbsp;</td>
<td><button @@10@@ onclick="change_env(10)" ><font color="#FF0000"><b>海外正式环境10</b></font></button>&nbsp;</td>
	]];

	for k, v in pairs( { 0, 1, 2, 10, 12 } ) do
		if  v == env_ then
			txt_ = string.gsub( txt_, "@@" .. v .. "@@", active_style_  )
		else
			txt_ = string.gsub( txt_, "@@" .. v .. "@@", ''  )
		end
	end


	FOO.add_txt( txt_ )

	FOO.add_txt( '</tr>' )
	FOO.add_txt( '</table>' )


	txt_ = [[
<br>
&nbsp;&nbsp; 请输入文字: <br><textarea type=text id='_input' style="width:700px; height:100px; border:solid 1px;" value="" ></textarea>&nbsp;&nbsp;
<button onclick="check_sens()" style="width:100px; height:100px;" > &nbsp;&nbsp; 检 测 &nbsp;&nbsp; </button>
<br>
&nbsp;&nbsp; 检测结果: <br><textarea type=text id='_output' style="width:700px; height:100px; border:solid 1px;" value="" ></textarea>
<br>

filter_cc:  敏感词计分，大于等于10分触发屏蔽
<br>
words:      触发了哪些敏感词, 下划线分割

]];


	FOO.add_txt( txt_ )
	FOO.add_txt( "<br><br><br><hr>最近玩家触发的敏感词列表<br>" )


	--"filter_score"
	local str_ = ""
	local file_name_ = "logs/miniw/filter_score_" .. os_date( "%Y%m%d" ) .. ".log"
	if  uu.file_exist( file_name_ ) then
		str_ = uu.os_exec( "tail -n 200 " .. file_name_ )
		str_ = string.gsub( str_, "\n", "\n<br>" )
	end
	FOO.add_txt( str_ )



	if  is_https_ then
		txt_ = [[
<script type="text/javascript">
	function change_env(env_){
		var url_ = "";
		if  (env_ == 1) {
			url_ = "https://120.24.64.132:8088/miniw/map/?https=1&";
		}
		else if  (env_ == 2) {
			url_ = "https://211.159.183.137:8088/miniw/map/?https=1&";
		}
		else if (env_ == 0) {
			url_ = "https://shequ.mini1.cn:8088/miniw/map/?https=1&";
		}
		else if (env_ == 10) {
			url_ = "https://hwshequ.mini1.cn:8088/miniw/map/?https=1&";
		}
		else if (env_ == 12) {
			url_ = "https://47.75.147.207:8088/miniw/map/?https=1&";
		}
		else{
			alert("此环境还未部署此功能");
			return;
		}
		window.location.href= url_ + "time=1564737726&uin=1166200&auth=r&act=sensCheckerPage";
	} ]]

	else
		txt_ = [[
<script type="text/javascript">
	function change_env(env_){
		var url_ = "";
		if  (env_ == 1) {
			url_ = "http://120.24.64.132:8087/miniw/map/?";
		}
		else if  (env_ == 2) {
			url_ = "http://211.159.183.137:8087/miniw/map/?";
		}
		else if (env_ == 0) {
			url_ = "http://shequ.mini1.cn:8087/miniw/map/?";
		}
		else if (env_ == 10) {
			url_ = "http://hwshequ.mini1.cn:8087/miniw/map/?";
		}
		else if (env_ == 12) {
			url_ = "http://47.75.147.207:8087/miniw/map/?";
		}
		else{
			alert("此环境还未部署此功能");
			return;
		}
		window.location.href= url_ + "time=1564737726&uin=1166200&auth=r&act=sensCheckerPage";
	} ]]

	end

	FOO.add_txt( txt_ )

	---js func
	txt_ = [[
	function check_sens(){
		var press_yes = true;
		if (press_yes) {
			//post请求
			var content_ = document.getElementById("_input").value;
			//alert(content_);
			var post_data_ = { content:content_ };
			ajax({
				url: "/miniw/map?time=1564737726&uin=1166200&auth=r&act=remote_check_sens&log=2",
				type: "POST",
				data: post_data_,
				dataType: "json",
				success: function (response, xml) {
					//alert( response );
					document.getElementById("_output").value = response;
				},
				fail: function (status) {
					alert( "服务器通讯失败，请联系海马 ......" );
				}
			});
		}
	}
</script>
]]

	FOO.add_txt( txt_ )

	FOO.add_txt( '</body></html>' )

	ngx.say( FOO.get_txt() )
end


-----------------------------------------------------------
---清理输出
FOO.clean_txt = function()
	FOO.show_txt = {};
end

---增加输出内容
FOO.add_txt = function( txt_ )
	FOO.show_txt[ #FOO.show_txt + 1 ] = txt_
end


---获得所有输出
FOO.get_txt = function()
	return table_concat( FOO.show_txt )
end


return FOO;